// pages/discover/index.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    discoverList: [],
    icoFocus: '',//图标拖动添加的效果
    icon: {
      icoPosX: "0", //横向 取值 left， right
      icoPosY: "370px", //纵向 取值 top bottom;
      icoLeft: "right",//水平坐标
      icoTop: 'top',//垂直坐标
    },
    icoTime: 0,//开始按住的时间
    icoMoving: false,//是否正在移动
    tmpPos: { x: 0, y: 0 }//缓存每次位置
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
//新增获取已有位置
    this.updatePos()
    this.data.sys = wx.getSystemInfoSync()
    //获取规则框真是宽度
    this.data.ruleTrueWidth = 120 / 750 * this.data.sys.windowWidth;
    this.data.ruleTrueHeight = 160 / 750 * this.data.sys.windowWidth;
    this.data.ruleTrueBottom = 140 / 750 * this.data.sys.windowWidth;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getActivities();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
     this.getActivities();
     wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '一起来拿演唱会门票吧！',
      path: "/pages/index/index",
      imageUrl: "http://resource.jiyilive.com/img/index/share3.jpg"
    }
  },

  checkDate: function (endTime) {
    //日期格式化
    var start_date = new Date();
    var end_date = new Date(endTime.replace(/-/g, "/"));
    // console.log(endTime,'|',start_date)
    //转成毫秒数，两个日期相减
    var days = end_date.getTime() - start_date.getTime();
    //转换成小时数 天数
    var day = parseInt(days / (1000 * 60 * 60));
    //do something
   // console.log("day = ", day);
    return day;
  },

  getActivities: function(){
    var _this=this
    wx.showLoading({title:"加载中"});
    app.apiRequest({
      url: '/activity/queryAll',
      data:{
        "status":0,
        "isDelete":0
      },
      success: res => {
        console.log("活动列表：",res);
        var arr=[]
        if(res.data instanceof Array){arr=res.data}
        if (res.data.data instanceof Array) { arr = res.data.data}
        if (res.data.data.data instanceof Array) { arr = res.data.data.data}
        
        var tres=arr//.reverse();        
        if(res.data.code == 0){
          for(var i=0; i<tres.length; i++){
            if (tres[i].totalNum === "0") { tres[i].totalNum=1;}
            tres[i].day = _this.checkDate(tres[i].endDate)
            var ts = _this.checkDate(tres[i].startDate)            
            if(ts<=0){ts=0;}
            // ts=0
            tres[i].sday = ts
            // console.log(tres[i].startDate,tres[i].sday)
          }
          _this.setData({
            discoverList: tres
          })
        }
        wx.hideLoading();
      }
    })
  },
  showActive:function(e){//进入活动函数
    var tid=e.target.dataset.id,
        aid = e.target.dataset.aid,
        sday=e.target.dataset.sday,
        sst=e.target.dataset.da;
    if(sday>0){
      wx.showToast({"title":"活动即将开启，敬请期待！","icon":"none"})
      return false;
    }
    wx.navigateTo({
      url: "performance/detail?id=" + tid + "&activityId=" + aid + "&sday=" + sday + "&sst=" + sst
    })
  },
  updatePos: function (ob) {//更新位置信息
    //新增获取rule图标位置    
    let tmpPos = wx.getStorageSync("rulePos") || '';
    if (ob) {//最优先，拖动时，频繁更新，
      this.setData({
        icon: ob
      })
    }
    else if (tmpPos != '') {//将已缓存的数据设置显示
      let nowp = JSON.parse(tmpPos);
      this.setData({
        icon: nowp
      })
    } else {//第一次进，使用默认数据初始化缓存
      let tinitPos = this.data.icon;
      wx.setStorageSync("rulePos", JSON.stringify(tinitPos));
    }
    // console.log(this.data.icon)
  },
  showTheRule: function () {//按完
    let ntime = new Date().getTime();
    if (ntime - this.data.icoTime < 100) {
      wx.navigateTo({
        url: "/pages/index/rule/rule"
      });
    } else {
      //还要计算贴近哪个框，就靠哪个框，另一个象限的值不变
      let tpos = this.data.icon,
        twpos = this.data.sys,
        truew = this.data.ruleTrueWidth,
        trueT = this.data.ruleTrueHeight,
        trueB = this.data.ruleTrueBottom;
      if (parseInt(tpos.icoPosX) < 0) { tpos.icoPosX = Math.abs(parseInt(tpos.icoPosX)); }
      if (parseInt(tpos.icoPosY) < 0) { tpos.icoPosY = Math.abs(parseInt(tpos.icoPosY)); }
      if (parseInt(tpos.icoPosX) > (twpos.windowWidth - truew)) { tpos.icoPosX = twpos.windowWidth - truew; }
      if (parseInt(tpos.icoPosY) > (twpos.windowHeight - truew)) { tpos.icoPosY = twpos.windowHeight - truew; }

      if (parseInt(tpos.icoPosX) < twpos.windowWidth / 2) {
        if (parseInt(tpos.icoPosY) < twpos.windowHeight / 2) {
          /*第一象限*/
          if (parseInt(tpos.icoPosX) < parseInt(tpos.icoPosY)) {//靠近x
            tpos.icoPosX = 0;
          } else {
            tpos.icoPosY = this.data.ruleTrueHeight + "px";
          }
        } else {
          //第四象限
          if (parseInt(tpos.icoPosX) < (twpos.windowHeight - parseInt(tpos.icoPosY))) {//靠近x
            tpos.icoPosX = 0;
          } else {
            tpos.icoPosY = twpos.windowHeight - truew * 0.7 + "px";
          }
        }
      } else {
        if (parseInt(tpos.icoPosY) < twpos.windowHeight / 2) {
          /*第二象限*/
          if ((twpos.windowWidth - parseInt(tpos.icoPosX)) < parseInt(tpos.icoPosY)) {//靠近x
            tpos.icoPosX = twpos.windowWidth - truew + "px";
          } else {
            tpos.icoPosY = trueT + "px";
          }
        } else {
          //第三象限
          if ((twpos.windowWidth - parseInt(tpos.icoPosX)) < (twpos.windowHeight - parseInt(tpos.icoPosY))) {//靠近x
            tpos.icoPosX = twpos.windowWidth - truew + "px";
          } else {
            tpos.icoPosY = twpos.windowHeight - truew * 0.7 + "px";
          }
        }
      }
      this.setData({ icon: tpos })
      wx.setStorageSync("rulePos", JSON.stringify(tpos));
    }
    this.data.icoTime = 0;
    this.data.icoMoving = false;
  },
  icoTouchStart: function (e) {
    this.data.icoMoving = true;
    let ntime = new Date().getTime();
    this.setData({
      icoTime: ntime
    });
    // console.log(e)
    let tpos = {
      x: e.changedTouches[0].pageX, y: e.changedTouches[0].pageY
    };
    this.setData({
      tmpPos: tpos
    });
  },
  icoTouchMove: function (e) {
    // if (this.data.icoMoving){      
    let pos = e.changedTouches[0];
    let tpos = this.data.tmpPos;
    let tob = this.data.icon
    // console.log(pos,tpos,tob);return;
    //计算位置
    tob.icoPosX = parseInt(tob.icoPosX) - (pos.pageX - tpos.x) + "px";
    tob.icoPosY = parseInt(tob.icoPosY) + (pos.pageY - tpos.y) + "px";
    // console.log(tob)
    tpos.x = pos.pageX;
    tpos.y = pos.pageY;
    this.updatePos(tob)
    // }else{
    //   let ntime = new Date().getTime();
    //   if (ntime - this.data.icoTime >= 300) {
    //     this.data.icoMoving=true;//可以开始移动了
    //   }
    // }
  }
})